package com.example.PriceCompare;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayInfo extends Activity{
	
	private TextView ProductName, PriceText, DistanceText;
	private TextView Location;
	private Button NavigateButton;
	private Button CallButton;
	private	double fromPointLat;
	private double fromPointLong;
	private String Product, Address, PhoneNum;
	private int Price;
	private double Distance;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.displayinfo);
		Bundle bundle = this.getIntent().getExtras();
		Product = bundle.getString("name");
		Address = bundle.getString("store");
		Price = bundle.getInt("price");
		Distance = bundle.getDouble("distance");
		PhoneNum = bundle.getString("phone");
		fromPointLat=bundle.getDouble("fromlat");
		fromPointLong=bundle.getDouble("fromlong");
		ProductName=(TextView)findViewById(R.id.Product);
		ProductName.setText(Product);
		Location=(TextView)findViewById(R.id.Location);
		Location.setText(Address);
		PriceText = (TextView)findViewById(R.id.Price);
		PriceText.setText("Rs. "+Integer.toString(Price));
		DistanceText = (TextView)findViewById(R.id.Distance);
		DistanceText.setText(Double.toString(Distance) +"km");
		NavigateButton = (Button)findViewById(R.id.Button01);
		CallButton = (Button)findViewById(R.id.Button02);
		
		NavigateButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Log.i("tag", "display act");
				LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
				LocationListener mlocListener = new MyLocationListener();
				mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
				Intent intent = new Intent(getApplicationContext(), PreMain.class);
				Bundle bundle = new Bundle();
				bundle.putDouble("fromlat", fromPointLat);
				Log.i("imp", " "+fromPointLat);
				bundle.putDouble("fromlong", fromPointLong);
				bundle.putString("toAdd", Address);
				intent.putExtras(bundle);

//				Toast.makeText( getApplicationContext(),fromPointLat+fromPointLong+Address,Toast.LENGTH_SHORT).show();

			startActivity(intent);
			}
		});
		
		CallButton.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {

			String phno ="tel:"+(PhoneNum.replace("-", ""));

			Intent callIntent= new Intent(Intent.ACTION_CALL);

			callIntent.setData(Uri.parse(phno));

			startActivity(callIntent);

			}

			});

	}
	
	
	public class MyLocationListener implements LocationListener{
	@Override
	public void onLocationChanged(Location loc){
//	loc.getLatitude();
//	loc.getLongitude();
/*
	fromPointLat = loc.getLatitude();
	fromPointLong = loc.getLongitude();
	Toast.makeText( getApplicationContext(),fromPointLat+fromPointLong+"  "+Address,Toast.LENGTH_SHORT).show();

	Intent intent = new Intent(getApplicationContext(),PreMain.class);
	Bundle bundle = new Bundle();
	bundle.putDouble("fromlat", fromPointLat);
	Log.i("imp", " "+fromPointLat);
	bundle.putDouble("fromlong", fromPointLong);
	bundle.putString("toAdd", Address);
	intent.putExtras(bundle);
	startActivity(intent);
	//	String Text = "My current location is: Latitud = "+loc.getLatitude()+" Longitud = "+ loc.getLongitude();
//	Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
*/	
	}

	@Override
	public void onProviderDisabled(String provider){
	Toast.makeText( getApplicationContext(),"Gps Disabled",	Toast.LENGTH_SHORT ).show();
	}

	@Override
	public void onProviderEnabled(String provider){
		
		Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras)
	{

	}

	}/* End of Class MyLocationListener */

	
}
